﻿
<?php 
	include("../../config/database.php");
	tep_db_connect() or die('Unable to connect to database server!');
	$method=$_REQUEST['action'];
	$flag="insert";
	if($method=="edit"){
		$proId=$_REQUEST['proId'];
		$flag="edit";
		// Load thong tin san pham 
		$sql="SELECT *  FROM products_description cd 
						inner join products_to_categories ptc on cd.products_id=ptc.products_id 
						inner join products p ON p.products_id=ptc.products_id 
						WHERE p.products_id=".$proId;									
		$result=mysql_query($sql);
		$row_edit=mysql_fetch_assoc($result);
	}
	
?>
<script src="editor/ckeditor.js" language="javascript"></script>

<link rel="stylesheet" type="text/css" media="all" href="../css/tipsy.css" />
<script src="../js/jquery.tipsy.js" language="javascript"></script>
<script>
	function Insert()
	{
		//alert($("#upload").val());
	}	
	function checkForm(form){
		var error=true;
		if($('#name').val()=="")
		{		
			$('#name').tipsy({fallback: "Product name isn't empty!" });
			$('#name').css('background-color','#F7819F');
			error=false;
		}
		if($('#price').val()==""||isNaN($('#price').val()))
		{		
			$('#price').tipsy({fallback: "Price isn't correct! (Ex:150000) !" });
			$('#price').css('background-color','#F7819F');
			error=false;
		}
		if(form=="insert"){
			if($('#upload').val()==""){
				$('#upload').tipsy({fallback: "Must upload image file !" });
				$('#upload').css('background-color','#F7819F');
				error=false;
			}
		}
		return error;
	}
</script>
<h2><?php if($flag=="edit")echo "Thông tin sản phẩm";else echo "Thêm sản phẩm"; ?></h2>     
         <div class="form">
         <form action="san_pham_config.php?method=<?php echo ($flag=="edit"?$flag."&proId=".$proId:$flag);?>" method="post" class="niceform" enctype="multipart/form-data" onsubmit="return checkForm('<?php echo ($flag=="edit"?$flag:$flag);?>')">
         
                <fieldset>
                    <dl>
                        <dt><label for="email" id="ab">Tên sản phẩm</label></dt>
                        <dd><input type="text" name="name" id="name" size="54" value="<?php echo (($flag=="edit")?$row_edit['products_name']:""); ?>" /></dd>
					</dl>
					<dl>
                        <dt><label for="password">Giá</label></dt>
                        <dd><input type="text" name="price" id="price"  value="<?php echo (($flag=="edit")?$row_edit['products_price']:""); ?>" size="54" /></dd>
                  </dl>
                    
                    
                    <dl>
						<?php 
							$sql="Select * from categories c inner join categories_description cd 
										on c.categories_id=cd.categories_id order by c.categories_id asc";
							$result=mysql_query($sql);
							
						?>
                        <dt><label for="gender">Chọn danh mục</label></dt>
                        <dd>
                            <select size="1" name="categories" id="categories">                             
							   <?php 
								
								   if($result)
								   {
										while($row=mysql_fetch_assoc($result))
										{
										?>
											<option <?php echo (($flag=="edit")?($row['categories_id']==$row_edit['categories_id']?"selected":""):"");?> value="<?php echo $row['categories_id'];?>"><?php echo $row['categories_name'];?></option>
										<?php 	
										}
								   }
							   ?>
                              
                            </select>
                        </dd>
                    </dl>                   
                    <dl>
                        <dt><label for="upload">Upload a File:</label><?php //if($flag=="edit")echo $row_edit['products_image'];?></dt>
                        <dd><input type="file" name="upload" id="upload" /></dd>
						
                    </dl>
                    
                    <dl>
                        <dt><label for="comments">Mô tả</label></dt>
                        <dd><textarea name="description"  id="description" rows="5" cols="36" style="display:none;">
							<?php if($flag=="edit")echo $row_edit['products_description'];?>
						</textarea>
							<script type="text/javascript" language="javascript">
								CKEDITOR.replace( 'description' );
							</script>  
						</dd>
                    </dl>
                    
                    <dl>
                        <dt><label></label></dt>
                        <dd>
                            <!--<input type="checkbox" name="interests[]" id="" value="" /><label class="check_label">I agree to the <a href="#">terms &amp; conditions</a></label>
							-->
                        </dd>
                    </dl>
                    
                     <dl class="submit">
                    <input type="submit" name="submit" id="submit" value="<?php echo ($flag=="edit"?"Save":"Insert");?>"/>
                     </dl>
                     
                     
                    
                </fieldset>
                
         </form>
         </div>  